package com.bsoft.gol.hbs.service.impl;

import com.bsoft.gol.hbs.dao.ProductDAO;
import com.bsoft.gol.hbs.dao.TenantProductsDAO;
import com.bsoft.gol.hbs.utils.Common;
import ctd.controller.exception.ControllerException;
import ctd.spring.boot.annotation.SsdevService;
import ctd.util.annotation.RpcService;
import hcn.base.Product;
import hcn.base.TenantProducts;
import hcn.service.base.ITenantProductsService;
import hcn.util.RpcUserToken;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import pcn.vo.web.Page;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by liuxj on 2016/8/1.
 */
@SsdevService("tenantProductsBean")
public class TenantProductsService implements ITenantProductsService {
    @Autowired
    private TenantProductsDAO tenantProductsDAO;
    @Autowired
    private ProductDAO productDAO;


    @Override
    @RpcService
    public String getProductTenantByTenantPCode(String tenantPCode) {
        List<TenantProducts> productTenantList = tenantProductsDAO.findProductTenant(tenantPCode);
        String desc = "暂无功能说明数据.";
        if (productTenantList != null && productTenantList.size() != 0) {
            TenantProducts productTenant = productTenantList.get(0);
            desc = productTenant.getDescription();
        }
        return desc;
    }

    /**
     * 医联体产品列表
     *
     * @param cloudId
     * @return
     * @throws ControllerException
     */
    @Override
    @RpcService
    public List<Map<String, Object>> queryTenantProductsInfo(String cloudId) throws ControllerException {
        List<Map<String, Object>> datas = tenantProductsDAO.getTenantProductsInfo(cloudId);
        Map<String, String> mapDic = new HashMap();
        mapDic.put("cloudId", "sys.tet.tenant");
        mapDic.put("productId", "hcn.base.dictionary.product");
        for (Map<String, Object> data : datas) {
            Common.mapPutKeyValue(data, mapDic);
        }
        return datas;
    }

    /**
     * 分页 医联体产品列表
     *
     * @param page
     * @return
     * @throws ControllerException
     */
    @Override
    @RpcService
    public Map<String, Object> queryTenantProductsList(Page page) throws ControllerException {
        Map<String, Object> map = new HashedMap();
        List<Map<String, Object>> datas = tenantProductsDAO.queryTenantProductsList(page);
        Long total = tenantProductsDAO.queryTenantProductsListCount(page.getCloudId());
        Map<String, String> mapDic = new HashMap();
//        mapDic.put("cloudId", "hcn.base.dictionary.orgcloud");
        mapDic.put("productId", "hcn.base.dictionary.product");
        for (Map<String, Object> data : datas) {
            Common.mapPutKeyValue(data, mapDic);
        }
        map.put("list", datas);
        map.put("total", total);
        return map;
    }

    /**
     * 医联体产品信息-根据医联体产品Id明细信息
     *
     * @param tenantPId
     * @return
     * @throws ControllerException
     */
    @Override
    @RpcService
    public Map<String, Object> getTenantProductsDetailById(int tenantPId) throws ControllerException {
        Map<String, Object> data = tenantProductsDAO.getTenantProductsDetailById(tenantPId);
        Map<String, String> mapDic = new HashMap<String, String>();
        mapDic.put("cloudId", "hcn.base.dictionary.orgcloud");
        mapDic.put("productId", "hcn.base.dictionary.product");
        Common.mapPutKeyValue(data, mapDic);
        return data;
    }

    /**
     * 医联体产品-保存
     *
     * @param tenantProducts
     * @return
     * @throws ControllerException
     */
    @Override
    @RpcService
    public int add(TenantProducts tenantProducts) throws ControllerException {
        TenantProducts exists = tenantProductsDAO.getTenantProductsByCodeForInsert(tenantProducts.getTenantPCode());
        if (exists != null)//医联体产品编码重复
        {
            throw new ControllerException(0, "医联体产品编码重复，请修改后重新保存！");
        }
        Product product = productDAO.getProductDetailById(tenantProducts.getProductId());
        if (product != null) {
            tenantProducts.setTenantPCode(RpcUserToken.getTenantId() + "." + product.getProductCode());
        }
        tenantProducts.setCreateDt(new Date());
        TenantProducts object_save = tenantProductsDAO.save(tenantProducts);
        return object_save.getTenantPId();
    }

    /**
     * 医联体产品-修改
     *
     * @param tenantProducts
     * @return
     * @throws ControllerException
     */
    @Override
    @RpcService
    public void update(TenantProducts tenantProducts) throws ControllerException {
        TenantProducts exists =
                tenantProductsDAO.getTenantProductsByCodeForUpdate(tenantProducts.getTenantPCode(), tenantProducts.getTenantPId());
        if (exists != null)//医联体产品编码重复
        {
            throw new ControllerException(0, "医联体产品编码重复，请修改后重新保存！");
        }
        tenantProducts.setLastModify(new Date());
        tenantProductsDAO.update(tenantProducts);
    }

    /**
     * 医联体产品-逻辑删除
     *
     * @param tenantPId
     * @throws ControllerException
     */
    @Override
    @RpcService
    public void delete(int tenantPId) throws ControllerException {
        tenantProductsDAO.delete(tenantPId);
    }

    public ProductDAO getProductDAO() {
        return productDAO;
    }

    public void setProductDAO(ProductDAO productDAO) {
        this.productDAO = productDAO;
    }
}
